package longrise.phone.com.bjjt_henan.bindinginformation;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.longrise.android.FormParameter;
import com.longrise.android.FrameworkManager;
import com.longrise.android.LFView;
import com.longrise.bjjt.updata.UpdataAPK;
import com.longrise.bjjt.util.PrefUtils;
import com.longrise.bjjt.util.UiUtil;

import longrise.phone.com.bjjt_henan.Constant.MsgConstant;
import longrise.phone.com.bjjt_henan.Constant.URLConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.DialogUtils;

/**
 * 设置界面
 */
public class SettingsView extends LFView implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private Context context;

    private View view;

    private ToggleButton trafficTBtn;

    private ToggleButton articleTBtn;

    private LinearLayout evaluateLayout;

    private LinearLayout feedbackLayout;

    private LinearLayout updateLayout;

    private LinearLayout aboutLayout;

    private UpdataAPK updataAPK;

    public SettingsView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (null == context) {
            return;
        }
        view = LayoutInflater.from(context).inflate(R.layout.view_settings, null);
        if (null == view) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("设置");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        trafficTBtn = (ToggleButton) view.findViewById(R.id.settings_tbtn_traffic);
        articleTBtn = (ToggleButton) view.findViewById(R.id.settings_tbtn_article);
        evaluateLayout = (LinearLayout) view.findViewById(R.id.settings_layout_evaluate);
        feedbackLayout = (LinearLayout) view.findViewById(R.id.settings_layout_feedback);
        updateLayout = (LinearLayout) view.findViewById(R.id.settings_layout_update);
        aboutLayout = (LinearLayout) view.findViewById(R.id.settings_layout_about);

        setListener(true);

        boolean isTra = PrefUtils.getBoolean(context, MsgConstant.TRAFFIC_NOTICE, false);
        trafficTBtn.setChecked(isTra);

        boolean isArt = PrefUtils.getBoolean(context, MsgConstant.ARTICLE_SEND, false);
        articleTBtn.setChecked(isArt);
    }

    private void setListener(boolean flag) {
        if (null != trafficTBtn) {
            trafficTBtn.setOnCheckedChangeListener(flag ? this : null);
        }
        if (null != articleTBtn) {
            articleTBtn.setOnCheckedChangeListener(flag ? this : null);
        }
        if (null != evaluateLayout) {
            evaluateLayout.setOnClickListener(flag ? this : null);
        }
        if (null != feedbackLayout) {
            feedbackLayout.setOnClickListener(flag ? this : null);
        }
        if (null != updateLayout) {
            updateLayout.setOnClickListener(flag ? this : null);
        }
        if (null != aboutLayout) {
            aboutLayout.setOnClickListener(flag ? this : null);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.settings_tbtn_traffic:
                PrefUtils.setBoolean(context, MsgConstant.TRAFFIC_NOTICE, isChecked);
                break;
            case R.id.settings_tbtn_article:
                PrefUtils.setBoolean(context, MsgConstant.ARTICLE_SEND, isChecked);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        LFView view = null;
        switch (v.getId()) {
            case R.id.settings_layout_evaluate://评价
                UiUtil.showToast(context, "评价功能暂未实现！");
                break;
            case R.id.settings_layout_feedback://反馈
                view = new FeedBackView(context);
                break;
            case R.id.settings_layout_update://更新
                checkVersion();
                break;
            case R.id.settings_layout_about://关于
                view = new VersionView(context);
                break;
            default:
                break;
        }
        if (null != view) {
            FrameworkManager.getInstance().showNewForm(context, view);
        }
    }

    /**
     * 检查版本更新
     */
    private void checkVersion() {
        if (updataAPK == null) {
            updataAPK = new UpdataAPK(context, context.getResources().getString(R.string.update_appname),
                    context.getResources().getString(R.string.kckpServerUrl), URLConstant.CHECKVERSION,
                    context.getResources().getString(R.string.updatekey));
            updataAPK.setCheckVersionListener(new UpdataAPK.CheckVersionListener() {
                @Override
                public void onCheckVersion(int mark) {
                    DialogUtils.closeDialog();
                    if (mark == 0) {
                        UiUtil.showToast(context, "当前已经是最新版本！");
                    }
                }
            });
        }
        DialogUtils.showDialog(context);
        updataAPK.checkVersion();
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        setListener(false);
        view = null;
    }
}
